package com.syqc.choose.teamcar

import androidx.lifecycle.MutableLiveData
import autodispose2.autoDispose
import com.syqc.comlib.auto.AutoDisposeViewModel
import com.syqc.comlib.utils.AppContext
import com.syqc.comlib.utils.CacheMMKV
import com.syqc.entity.TeamsAndCars
import com.syqc.monitor.R
import com.syqc.net.ServiceHelper

/**
 *@author joker
 *@time   2024/5/31 11:17
 *@desc
 */
class TeamsVM : AutoDisposeViewModel() {
    private val teamIds = arrayListOf<String>()
    private val teamNames = arrayListOf<String>()
    private var pid = "0"

    private val rootName = AppContext.getDescById(R.string.desc_choose_root)
    val teamsLive = MutableLiveData<ArrayList<TeamsAndCars>>()
    val searchLive = MutableLiveData<ArrayList<TeamsAndCars>>()
    val nameLive = MutableLiveData<String>()

    init {
        teamIds.clear()
        teamIds.add(pid)
        teamNames.add(rootName)
        initTeam()
    }

    /**
     * Get teams cars  获取车队和车辆信息
     *
     */
    fun getTeamsCars() {
        ServiceHelper.getRepository().getTeamsAndCars().autoDispose(this).subscribe({
            TeamCarsUtil.handlerTeamCars(it, false)
            val teams = TeamCarsUtil.getTeams(pid)
            if (teams.isEmpty()) {
                toast(R.string.toast_no_child_team)
            } else {
                teamsLive.postValue(teams)
                searchLive.postValue(teams)
            }
        }, error)
    }

    /**
     * Get team car 获取车队
     *
     * @param teamId
     * @param teamName
     */
    fun getTeamCar(teamId: String, teamName: String) {
        pid = teamId
        if (teamId == "0") {
            reset()
        } else {
            val teams = TeamCarsUtil.getTeams(pid)
            if (teams.isEmpty()) {
                toast(R.string.toast_no_child_team)
            } else {
                teamIds.add(teamId)
                teamNames.add(teamName)
                nameLive.postValue(teamName)
                teamsLive.postValue(teams)
            }
        }
    }

    /**
     * Reset  重置
     *
     */
    private fun reset() {
        teamIds.clear()
        teamNames.clear()
        teamIds.add("0")
        teamNames.add(rootName)
        nameLive.postValue(rootName)
        teamsLive.postValue(TeamCarsUtil.getTeams(pid))
    }

    /**
     * Get previous 上一辆
     *
     */
    fun getPrevious() {
        if (teamIds.size <= 1) {
            toast(R.string.desc_root_team)
            return
        }
        if (pid == "0") {
            getTeamCar("0", "")
            return
        }
        teamIds.removeAt(teamIds.lastIndex)
        teamNames.removeAt(teamNames.lastIndex)
        nameLive.postValue(teamNames.last())
        pid = teamIds.last()
        teamsLive.postValue(TeamCarsUtil.getTeams(pid))
    }

    /**
     * Get root team  获取根车队
     *
     */
    fun getRootTeam() {
        if (teamIds.isEmpty() || pid == "0") {
            toast(R.string.desc_root_team)
            return
        }
        pid = "0"
        reset()
    }

    // 查询车队
    fun searchTeam(key: String) {
        val upKey = key.uppercase()
        val search = TeamCarsUtil.search(upKey)

        if (search.isEmpty()) {
            toast(R.string.toast_no_team)
            return
        }
        searchLive.postValue(search)
    }


    /**
     * 保存最后选中的数据
     *
     */
    fun saveData() {
        var ids = ""
        teamIds.forEach {
            ids = "$ids,$it"
        }
        var nameS = ""
        teamNames.forEach {
            nameS = "$nameS,$it"
        }
        CacheMMKV.encode("ChooseIds", ids.substring(1))
        CacheMMKV.encode(
            "ChooseNames", nameS.substring(1)
        )
    }

    /**
     * Init team  车队初始化
     *
     */
    fun initTeam() {
        val ids = CacheMMKV.decodeString("ChooseIds")
        val nameS = CacheMMKV.decodeString("ChooseNames")
        if (ids.isEmpty()) {
            teamIds.add("0")
            teamNames.add(rootName)
        } else {
            teamIds.clear()
            teamIds.addAll(ids.split(","))
            teamNames.clear()
            teamNames.addAll(nameS.split(","))
        }
        pid = teamIds[teamIds.lastIndex]
        nameLive.postValue(teamNames[teamNames.lastIndex])
    }

}